<script setup>
import { computed, ref } from 'vue'

const props = defineProps({
  products: Array
})
//定义一个用于记录当前显示的产品索引的响应式变量
const currIndex = ref(0)
//计算属性，生成产品列表，每个子数组最多包含四个产品
const productsList = computed(() => {
  const list = [[]]
  props.products.forEach(image => {
    if (list[list.length - 1].length === 4) {
      list.push([]) //当前子数组满4个产品时，创建新的子数组
    }
    if (list[list.length - 1].length < 4) {
      list[list.length - 1].push(image) //将产品加入当前子数组
    }
  })
  return list
})
//切换到前一个产品
const onPrev = () => {
  if (currIndex.value === 0) {
    currIndex.value = productsList.value.length - 1 //当前索引为0时，循环到最后一个子数组
  } else {
    currIndex.value = currIndex.value - 1 //否则索引减1
  }
}
//切换到下一个产品
const onNext = () => {
  if (currIndex.value === productsList.value.length - 1) {
    currIndex.value = 0 //当前索引为最后一个子数组时，循环到第一个子数组
  } else {
    currIndex.value = currIndex.value + 1 //否则索引加1
  }
}

const onIndicatorClick = index => {
  currIndex.value = index
}
</script>

<template>
  <div class="goods-carousel">
    <div class="header">
      <el-icon><Present /></el-icon>
      <span>猜你喜欢</span>
    </div>
    <div class="body">
      <a href="javascript:;" class="carousel-btn prev" @click="onPrev">
        <el-icon><ArrowLeft /></el-icon>
      </a>
      <ul class="carousel-body">
        <li
          :class="currIndex === index ? 'carousel-item active' : 'carousel-item'"
          v-for="(list, index) in productsList"
          :key="index"
        >
          <div class="slider">
            <a v-for="(product, index) in list" :key="index">
              <img :src="product.picture" alt="" />
              <p class="name">{{ product.name }}</p>
              <p class="price">¥{{ product.price }}</p>
            </a>
          </div>
        </li>
      </ul>
      <a href="javascript:;" class="carousel-btn next" @click="onNext">
        <el-icon><ArrowRight /></el-icon>
      </a>
    </div>
    <div class="carousel-indicator">
      <span
        v-for="(list, index) in productsList"
        :key="index"
        :class="currIndex === index && 'active'"
        @click="onIndicatorClick(index)"
      ></span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.goods-carousel {
  width: 1240px;
  height: 450px;
  background-color: #fff;
  position: relative;
  .header {
    height: 40px;
    font-size: 30px;
    display: flex;
    align-items: center;
    padding: 20px 30px;
    color: #27ba9b;
    span {
      color: #333;
      margin-left: 5px;
      font-size: 20px;
    }
  }
  ul {
    list-style: none;
  }
  .carousel-body {
    width: 100%;
    height: 290px;
    .carousel-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 100px;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.active {
        opacity: 1;
        z-index: 1;
      }
    }
    .slider {
      display: flex;
      justify-content: space-around;
      padding: 0 40px;
      a {
        text-decoration: none;
        color: #333;
        outline: none;
        width: 240px;
        text-align: center;
        img {
          width: 200px;
          height: 200px;
        }
        .name {
          font-size: 16px;
          color: #666;
          padding: 0 40px;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
        .price {
          font-size: 16px;
          color: #cf4444;
          margin-top: 15px;
        }
      }
    }
  }
  .carousel-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    position: absolute;
    z-index: 2;
    text-align: center;
    line-height: 44px;
    transition: all 0.5s;
    text-decoration: none;
    outline: none;
    top: 180px;
    opacity: 1;
    background: transparent;
    color: #ddd;
    &.prev {
      left: 20px;
    }
    &.next {
      right: 20px;
    }
    i {
      font-size: 30px;
    }
  }
  .carousel-indicator {
    position: absolute;
    left: 0;
    bottom: 30px;
    z-index: 2;
    width: 100%;
    text-align: center;
    span {
      display: inline-block;
      width: 12px;
      height: 12px;
      background: rgba(0, 0, 0, 0.2);
      border-radius: 50%;
      cursor: pointer;
      & ~ span {
        margin-left: 12px;
      }
      &.active {
        background: #27ba9b;
      }
    }
  }
}
</style>

<!-- 
.carousel-body {
    width: 100%;
    height: 290px;
    .carousel-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 50px;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.active {
        opacity: 1;
        z-index: 1;
      }
    }
    .slider {
      display: flex;
      justify-content: space-around;
      padding: 0 40px;
      a {
        text-decoration: none;
        color: #333;
        outline: none;
        width: 240px;
        text-align: center;
        img {
          padding: 20px;
          width: 230px;
          height: 230px;
        }
        .name {
          font-size: 16px;
          color: #666;
          padding: 0 40px;
          text-overflow: ellipsis;
          overflow: hidden;
          white-space: nowrap;
        }
        .price {
          font-size: 16px;
          color: #cf4444;
          margin-top: 15px;
        }
      }
    }
  }
-->
